<?php


namespace app\api\controller;


use think\Db;

class Item extends Lib
{

    // 自定义
    public function zbx_item_get()
    {
        $hostids = Db::name('hosts')->column('hostid,deptid');

        $params = [
            "output" => ["itemid", "hostid", "host", "name"],
            "hostids" => array_keys($hostids)
        ];
        $result = json_decode(self::RPC('item.get', $params), true);
        $data = $result['data'];

        if ($data){
            $add_data = [];
            foreach ($data as $k=>$v){
                $is_item = Db::name('items')->where('itemid',$v['itemid'])->find();
                if (!$is_item){
                    $add_data[$k] = $v;
                    $add_data[$k]['deptid'] = $hostids[$v['hostid']];
                }
            }
            if ($add_data){
                $add = Db::name('items')->insertAll($add_data);
                if ($add){
                    return true;
                }else{
                    return false;
                }
            }else{
                return true;
            }
        }else{
            return true;
        }
    }
}